<?php
/**
 * Created by PhpStorm.
 * User: guokunsheng
 * Date: 2022/5/5
 * Time: 上午11:23
 */

namespace app\web\controller;

use app\api\controller\v2\Auth;
use app\common\cachemodel\lucky\AppLuckyDraw;
use app\common\cachemodel\lucky\AppUserLuckyInfo;
use think\Env;

/**
 * 幸运星抽奖
 * Class Lotto
 * @package app\web\controller
 */
class Lotto extends Auth
{
    /**
     * 奖品列表
     * type 1=>卡券,2=>准币,3=>实物,4=>装扮,5=>下次好运
     * @var array[]
     */
    protected $awardList = [
        [
            'id' => 1,
            'name' => '限定徽章',
            'des' => '限定徽章30天有效期',
            'img' => 'https://oss.goddessxzns.com/test/lucky_star/lotto-1.png',
            'tip' => 'https://oss.goddessxzns.com/test/lucky_star/lotto-1-tip.png',
            'type' => 4,
        ],
        [
            'id' => 2,
            'name' => '一生报告券',
            'des' => '一生报告八折券',
            'img' => 'https://oss.goddessxzns.com/test/lucky_star/lotto-2.png',
            'tip' => 'https://oss.goddessxzns.com/test/lucky_star/lotto-2-tip.png',
            'type' => 1,
        ],
        [
            'id' => 3,
            'name' => '2888准币',
            'des' => '可用于下单准了商品',
            'img' => 'https://oss.goddessxzns.com/test/lucky_star/lotto-5.png',
            'tip' => 'https://oss.goddessxzns.com/test/lucky_star/lotto-5-tip.png',
            'type' => 2,
        ],
        [
            'id' => 4,
            'name' => '下次好运',
            'des' => '下次再来哦～',
            'img' => 'https://oss.goddessxzns.com/test/lucky_star/lotto-3.png',
            'tip' => 'https://oss.goddessxzns.com/test/lucky_star/lotto-3-tip.png',
            'type' => 5,
        ],
        [
            'id' => 5,
            'name' => '准了口罩',
            'des' => '需填写收货地址',
            'img' => 'https://oss.goddessxzns.com/test/lucky_star/lotto-7.png',
            'tip' => 'https://oss.goddessxzns.com/test/lucky_star/lotto-7-tip.png',
            'type' => 3,
        ],
        [
            'id' => 6,
            'name' => '幸运星x12',
            'des' => '获得12颗幸运星',
            'img' => 'https://oss.goddessxzns.com/test/lucky_star/lotto-4.png',
            'tip' => 'https://oss.goddessxzns.com/test/lucky_star/lotto-4-tip.png',
        ],
        [
            'id' => 7,
            'name' => '下次好运',
            'des' => '下次再来哦～',
            'img' => 'https://oss.goddessxzns.com/test/lucky_star/lotto-3.png',
            'tip' => 'https://oss.goddessxzns.com/test/lucky_star/lotto-3-tip.png',
            'type'
        ],
        [
            'id' => 8,
            'name' => '2888准币',
            'des' => '可用于下单准了商品',
            'img' => 'https://oss.goddessxzns.com/test/lucky_star/lotto-5.png',
            'tip' => 'https://oss.goddessxzns.com/test/lucky_star/lotto-5-tip.png',
        ],
        [
            'id' => 9,
            'name' => '下次好运',
            'des' => '下次再来哦～',
            'img' => 'https://oss.goddessxzns.com/test/lucky_star/lotto-3.png',
            'tip' => 'https://oss.goddessxzns.com/test/lucky_star/lotto-3-tip.png',
        ],
        [
            'id' => 10,
            'name' => '50元抵扣券',
            'des' => '可用于下单准了商品',
            'img' => 'https://oss.goddessxzns.com/test/lucky_star/lotto-6.png',
            'tip' => 'https://oss.goddessxzns.com/test/lucky_star/lotto-6-tip.png',
        ],
        [
            'id' => 11,
            'name' => '下次好运',
            'des' => '下次再来哦～',
            'img' => 'https://oss.goddessxzns.com/test/lucky_star/lotto-3.png',
            'tip' => 'https://oss.goddessxzns.com/test/lucky_star/lotto-3-tip.png',
        ],
        [
            'id' => 12,
            'name' => '准了口罩',
            'des' => '需填写收货地址',
            'img' => 'https://oss.goddessxzns.com/test/lucky_star/lotto-7.png',
            'tip' => 'https://oss.goddessxzns.com/test/lucky_star/lotto-7-tip.png',
        ],
    ];

    /**
     * 每次抽奖需要消耗的幸运星数量
     * @var int
     */
    protected $price = 10;

    /**
     * 幸运星抽奖页面展示
     */
    public function index()
    {
        $header = [
            'version' => $this->request->header('version'),
            'time' => $this->request->header('time'),
            'distinct-id' => $this->request->header('distinct-id'),
            'phone-version' => $this->request->header('phone-version'),
            'app-type' => $this->request->header('app-type'),
            'did' => $this->request->header('did'),
            'access-token' => $this->request->header('access-token'),
            'model' => $this->request->header('model'),
            'sign' => $this->request->header('sign'),
        ];

        $award_list = (new AppLuckyDraw())->getDrawList();

        // 处理后台关闭一个奖品时，使用下次好运填补
        $awl = [];
        if (count($award_list) < 12)
        {
            for($i = 1; $i <= 12; $i++)
            {
                foreach ($award_list as $k => $v)
                {
                    if ($i === $v['index'] * 1)
                    {
                        $awl[$i] = [
                            'name' => $v['name'],
                            'des' => $v['des'],
                            'pic' => $v['pic'],
                            'img' => $v['img'],
                        ];
                        break;
                    }
                }
                if (!isset($awl[$i]))
                {
                    $awl[$i] = [
                        'name' => '下次好运',
                        'des' => '本次没有抽中任何奖品，再接再厉哦！',
                        'pic' => '/uploads/20220530/2fd89ddbc736a4f9ab882eec1358e74e.png',
                        'img' => '/uploads/20220530/382234b4791c5657d11d871468caf5ac.png',
                    ];
                }
            }
            $award_list = $awl;
        }
        $luckyInfo = (new AppUserLuckyInfo())->IndexGetUserLuckyInfo($this->user['id']);
        $notice_list = (new AppLuckyDraw())->getDrawRecordCache();

        $data = [
            'star_num' => $luckyInfo['lucky_num'],
//            'notice_list' => [
//                'Mark获得了“限定徽章”',
//                '海涛获得了“2888准币”',
//                'Hotchass获得了“限定徽章”',
//                '一条大璐璐获得了“2888准币”',
//                '哈哈获得了“限定徽章”',
//                'Seunk获得了“2888准币”',
//                '哭哭7获得了“限定徽章”',
//                '菲菲8获得了“2888准币”',
//                '哭哭9获得了“限定徽章”',
//                '菲菲10获得了“2888准币”',
//                '哭哭11获得了“限定徽章”',
//                '菲菲12获得了“2888准币”',
//                '哭哭13获得了“限定徽章”',
//                '菲菲14获得了“2888准币”',
//                '哭哭15获得了“限定徽章”',
//                '菲菲16获得了“2888准币”',
//                '哭哭17获得了“限定徽章”',
//                '菲菲18获得了“2888准币”',
//                '哭哭19获得了“限定徽章”',
//                '菲菲20获得了“2888准币”',
//            ],
//            'award_list' => $this->awardList,
            'notice_list' => $notice_list,
            'award_list' => $award_list,
            'price' => $this->price, // 多少幸运星抽一次奖
            'header' => json_encode($header),
            'oss' => Env::get('host.photo')
        ];
        return $this->fetch('', $data);
    }
}
